using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;


using System.Data.Linq;
using System.Linq;

using DataLayer;

namespace BusinessLayer.Activities
{
   public partial class Delete_TabLink : System.Workflow.ComponentModel.Activity
   {
      public Delete_TabLink()
      {
			//InitializeComponent();
      }

      private static DependencyProperty TabIdProperty = DependencyProperty.Register("TabId", typeof(int), typeof(Delete_TabLink));
      public int TabId
      {
         get { return (int)base.GetValue(TabIdProperty); }
         set { base.SetValue(TabIdProperty, value); }
      }

      private static DependencyProperty UserGuidProperty = DependencyProperty.Register("UserGuid", typeof(Guid), typeof(Delete_TabLink));
      public Guid UserGuid
      {
         get { return (Guid)base.GetValue(UserGuidProperty); }
         set { base.SetValue(UserGuidProperty, value); }
      }


      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
      {
         using (var db = DatabaseHelper.GetDashboardData())
         {
            /*
             * Check UserTab, if roleid == individual, 
             * delete the tab itself (usertab and other tab related info will be cascaded)
             * 
             * otherwise
             * mark the usertab as deleted
             */

            var usertabs = from p in db.UserTabs
                           where p.TabId == TabId && p.UserID == UserGuid
                           select p;

            foreach (var usertab in usertabs)
            {
               usertab.Deleted = true;

               //no need bother about other thing because this is a shared tab, i'm only removing my reference to this tab
            }

            db.SubmitChanges();
         }
         return ActivityExecutionStatus.Closed;
      }
   }
}